package com.guiwu.gteway.industryapp.ChangePassword;
import android.support.annotation.NonNull;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.guiwu.gteway.industryapp.Api.GuiwuApi;
import com.guiwu.gteway.industryapp.Api.GuiwuApiCallback;
import com.guiwu.gteway.industryapp.util.ApiJsonUtil;
import com.guiwu.gteway.industryapp.util.JsonResult;

import org.json.JSONException;
import org.json.JSONObject;

import static com.google.common.base.Preconditions.checkNotNull;
public class ChangePresenter implements ChangePasswordContract.Presenter {

    private ChangePasswordContract.View mChangeView;

    public ChangePresenter(ChangePasswordContract.View changeView){
        checkNotNull(changeView);
        mChangeView = changeView;
    }

    @Override
    public void askChangePsw(String newPassword, String phoneNum) {
        checkNotNull(newPassword);
        checkNotNull(phoneNum);
        mChangeView.showLoadingIndicator(true);
        JSONObject requestContent = new JSONObject();
        try {
            requestContent.putOpt("mobile",phoneNum);
            requestContent.putOpt("password",newPassword);
            GuiwuApi.changePassword(requestContent.toString(), new GuiwuApiCallback() {
                @Override
                public void onSuccess(String response) {
                    JsonObject object = new JsonParser().parse(response).getAsJsonObject();
                    int error = object.get("errno").getAsInt();
                    if (error == 0){
                        mChangeView.onChangePassword(true,"修改成功");
                    }else {
                        mChangeView.onChangePassword(false,object.get("error").getAsString());
                    }
                }

                @Override
                public void onFailed(Exception e) {
                    mChangeView.onChangePassword(false,e.getMessage());
                }
            });


        } catch (JSONException e) {
            e.printStackTrace();
        }finally {
            mChangeView.showLoadingIndicator(false);
        }
    }

    @Override
    public void start() {

    }


    @Override
    public void getVerificationCode(@NonNull String poneNumber) {
        checkNotNull(poneNumber,"电话号码不能为空");
        try {
            String requestBody = assembleCodeJson(poneNumber);
            GuiwuApi.getVertifyCode(requestBody, new GuiwuApiCallback() {
                @Override
                public void onSuccess(String response) {
                    JsonResult result = ApiJsonUtil.getData(response);
                    if (result.getJsonElement() != null){
                        JsonObject object = result.getJsonElement().getAsJsonArray().get(0).getAsJsonObject();
                        String code = object.get("code").getAsString();
                        mChangeView.onGetVerifyCode(code);
                    }else {
                        mChangeView.showGetVerifyCodeError(result.getErrorMsg());
                    }
                }

                @Override
                public void onFailed(Exception e) {
                    mChangeView.showGetVerifyCodeError(e.getMessage());
                }
            });

        } catch (JSONException e) {
            e.printStackTrace();
            mChangeView.showGetVerifyCodeError(e.getMessage());
        }
    }

    //组装短信验证码请求
    private String assembleCodeJson(String poneNumber)throws JSONException{
        JSONObject requestContent = new JSONObject();
        requestContent.putOpt("mobile",poneNumber);
        return requestContent.toString();
    }


}
